﻿using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
using WebMiddleware.Views;

namespace WebMiddleware
{
    public class WpfHostedService : BackgroundService
    {
        private readonly WpfApplication _app;

        private readonly IHostApplicationLifetime _applicationLifetime;
        private readonly IMainWindow window;

        public WpfHostedService(WpfApplication app, IHostApplicationLifetime applicationLifetime, IMainWindow window)
        {
            _app = app;
            _applicationLifetime = applicationLifetime;
            this.window = window;
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _app.Run(window as MainWindow);
            _applicationLifetime.StopApplication();

            return Task.CompletedTask;
        }
    }
}
